package com.testin.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import com.testin.model.ModelPage;

import com.testin.dao.ReadModelDao;
import com.testin.dao.DataAccessException;
import com.testin.utils.DBUtils;

public class ReadModelDaoImpl implements  ReadModelDao {
	
	public List<ModelPage> getModelPageList() {
		
		String sql = "select t.id as id ,m.model_name as model_name,t.test_text as test_text from  task_model t, model_type m where  t.type_id=m.id and m.flag=0";

		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			conn = DBUtils.getConnection();
			ps = conn.prepareStatement(sql);
			rs = ps.executeQuery();

			return assembleAccount(rs);
		} catch (Exception e) {
			throw new DataAccessException("##SQL Error##", e);
		} finally {
			DBUtils.close(rs, ps, conn);
		}
	}
	
	private List<ModelPage> assembleAccount(ResultSet rs) throws SQLException {
		List<ModelPage> modelPageList = new ArrayList<ModelPage>();
		while (rs.next()) {
			ModelPage modelPage = new ModelPage();
			modelPage.setId(Integer.parseInt(rs.getString("id")));
			modelPage.setKey(rs.getString("model_name"));
			modelPage.setValue(rs.getString("test_text"));
			modelPageList.add(modelPage);
		}

		return modelPageList;
	}

}
